/**
 * 一维数组转树
 * @param {*} data 
 * @param {*} id 
 * @param {*} parentId 
 * @param {*} children 
 * @param {*} rootId 
 * @returns 
 */
const dmHandleTree = (data, id, parentId, children, rootId) => {
  id = id || 'id'
  parentId = parentId || 'parentId'
  children = children || 'children'
  rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
  // 对源数据深度克隆
  let cloneData = JSON.parse(JSON.stringify(data))
  // 循环所有项
  let treeData = cloneData.filter(father => {
    let branchArr = cloneData.filter(child => {
      // 返回每一项的子级数组
      return father[id] == child[parentId]
    })
    father.children = branchArr.length > 0 ? branchArr : ''
    // 返回第一层
    return father[parentId] == rootId
  })
  return treeData !== '' ? treeData : data
}

/**
 * 根据树的某节点获取树的所有父节点
 * @param {*} datasource // 树形结构数据
 * @param {*} node // 节点值
 * @param {*} id // 唯一标识默认id
 * @param {*} children // 子节点默认children
 * @param {*} ob // 返回参数如果为true返回对象，否则返回单个属性
 * @returns 
 */
const dmFindParent = (datasource, node, id = 'id', children = 'children', ob = false) => {
  let targetData = {};
  let loops = (data = [], parent) => {
    return data.map((item) => {
      let value = item[id]
      const node = {
        value,
        parent,
        item
      }
      targetData[item[id]] = node;
      node.children = loops(item[children], node);
      return
    })
  };
  let getNode = (value) => {
    let node = [];
    if (targetData[value]) {
      let currentNode = targetData[value];
      if (ob) {
        node.push(currentNode.item);
      } else {
        node.push(currentNode.value);
      }

      if (currentNode.parent) {
        node = [...getNode(currentNode.parent.value), ...node];
      }
    }
    return node
  };
  loops(datasource)
  return getNode(node)
}

/**
 * 根据树的某个节点获取树的所有子节点
 * @param {*} datasource // 树形结构数据
 * @param {*} node // 节点值
 * @param {*} id // 唯一标识默认id
 * @param {*} children // 子节点默认children
 * @param {*} ob // 返回参数如果为true返回对象，否则返回单个属性
 * @returns 
 */
const dmFindChild = (datasource, node, id = 'id', children = 'children', ob = false) => {
  let getChild = (nodes, item, arr) => {
    for (let el of nodes) {
      if (el[id] === item) {
        if (ob) {
          arr.push(el);
        } else {
          arr.push(el[id]);
        }
        if (el[children]) {
          childNodesDeep(el[children], arr);
        }
      } else if (el[children]) {
        getChild(el[children], item, arr);
      }
    }
    return arr;
  }
  let childNodesDeep = (nodes, arr) => {
    if (nodes)
      nodes.forEach((ele) => {
        if (ob) {
          arr.push(ele);
        } else {
          arr.push(ele[id]);
        }
        if (ele[children]) {
          this.childNodesDeep(ele[children], arr);
        }
      }
      );
  }
  return getChild(datasource, node, [])
}

/**
 * 找树形数据的指定节点
 * @param {*} tree // 树形数组必传
 * @param {*} id  // 默认0
 * @param {*} children // 默认children
 * @param {*} pk // 默认id
 * @returns 
 */
const dmFindNode = (tree,id='0',children ='children',pk='id') => {
  if (!tree || !Array.isArray(tree) || tree.length <= 0) {
    return 'tree is empty or not an array'
  }
  let res = null
  for (let i = 0; i < tree.length; i++) {
    let ele = tree[i]
    ele[pk] === id ? res = ele : ''
    if (res) break;
    if (ele[children] && ele[children].length) {
      res = this.getItemByIdInTree(ele[children], id)
    }
  }
  return res
}

export {
  dmHandleTree,
  dmFindParent,
  dmFindChild,
  dmFindNode,
}